//
//  GYBuyDetailView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/3.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYBuyDetailView.h"
#import "GYBuyDetailCell.h"
#import "GYBuyDetailMiddleCell.h"
@interface GYBuyDetailView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)GYHomeBuyProductInfoModel* inforModel;
@property(nonatomic,assign)BOOL isCollect;
@end

@implementation GYBuyDetailView

-(GYHomeBuyProductInfoModel *)inforModel{

    if (!_inforModel) {
        _inforModel=[[GYHomeBuyProductInfoModel alloc] init];
    }
    return _inforModel;
}
-(GCBaseTableView *)myTable
{
    if (!_myTable) {
        _myTable=[[GCBaseTableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
        _myTable.delegate=self;
        _myTable.dataSource=self;
        _myTable.separatorStyle=UITableViewCellSeparatorStyleNone;
        _myTable.backgroundColor=[UIColor whiteColor];
    }
    return _myTable;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.myTable];
    }
    return self;
}
#pragma mark - delegate
#pragma mark - table delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0)
    {
        static NSString *identify = @"sectioncell";
        GYBuyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[GYBuyDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
       // cell.model=_inforModel;
        if (_inforModel)
        {
            [cell setModel:_inforModel];
        }
        cell.collectBtn.selected=_isCollect;
         [cell.collectBtn setEnlargeEdge:10];
        [cell.collectBtn addTarget:self action:@selector(butnPress:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    
    if (indexPath.section==1)
    {
        static NSString *identify = @"section1cell";
        GYBuyDetailMiddleCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[GYBuyDetailMiddleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
       // cell.model=_inforModel;
        return cell;
    }
    static NSString *identify = @"section2cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
    
    UILabel* markLab=[[UILabel alloc]initWithFrame:WFCGRectMake(12.5, 19.5, 5, 15)];
    markLab.backgroundColor=Main_Color_Red;
    [cell.contentView addSubview:markLab];
    
    UILabel* titleLab=[[UILabel alloc]init];

    titleLab.textColor=Main_Color_Black;
    titleLab.font=SYSTEMFONT(15);
    titleLab.frame=WFCGRectMake(28, 16.5, 200, 21);
    titleLab.text=@"需求描述";
    [cell.contentView addSubview:titleLab];
    
    UIImageView* _headIV=[UIImageView new];
    [_headIV sd_setImageWithURL:[NSURL URLWithString:_inforModel.imgpath] placeholderImage:nil];
    _headIV.x=WFCGFloatX(15);
    _headIV.y=WFCGFloatY(51);
    _headIV.width=WFCGFloatX(345);
    _headIV.height=WFCGFloatY(300);
    [cell.contentView addSubview:_headIV];
    
//    UILabel* contentLab=[[UILabel alloc]init];
//    contentLab.numberOfLines=0;
//    contentLab.textColor=Main_Color_Gray;
//    contentLab.font=SYSTEMFONT(14);
//    contentLab.frame=WFCGRectMake(15, 51, 375-30, 21);
//    contentLab.text=@"我不要描述";
//    [cell.contentView addSubview:contentLab];
    
    //description
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return WFCGFloatY(184);
 
    }
    if (indexPath.section==1) {
        return WFCGFloatY(121);
    }

    return WFCGFloatY(51+300);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[UIView new];
    view.backgroundColor=RGBACOLOR(247, 247, 247, 1);
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return .0001f;
    }
    return 6;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1)
    {
        if (![[GYLoginViewModel shareInterface] loginstatus])
        {
            [[GCPresenter shareInterface] loadLogin];
            return;
        }
    }
}

-(void)refreshTable:(GYHomeBuyProductInfoModel *)model
{
    _inforModel=model;
    [self.myTable reloadData];
    
}
-(void)refreshCollect:(BOOL)collect
{
    _isCollect=collect;
    [self.myTable reloadData];
    
}
-(void)butnPress:(UIButton*)sender
{
    if (![[GYLoginViewModel shareInterface] loginstatus])
    {
        [[GCPresenter shareInterface] loadLogin];
        return;
    }
    if (!_isCollect)
    {
        [FTIndicator showSuccessWithMessage:@"收藏成功"];
        _isCollect=YES;
    }else
    {
        [FTIndicator showSuccessWithMessage:@"取消收藏"];
        _isCollect=NO;
    }
    [self.myTable reloadData];
//    if ([[GYLoginViewModel shareInterface] roleType]!=1) {
//        [FTIndicator showErrorWithMessage:@"您还不是革优认证会员,请注册并认证~"];
//        return;
//    }
    
//    if (![[GYLoginViewModel shareInterface] loginstatus])
//    {
//        [[GCPresenter shareInterface] loadLogin];
//        return;
//    }
//    @WeakSelf;
//    NSString *interface = @"collect";
//    NSMutableDictionary* para=[[NSMutableDictionary alloc] init];
//    [para setObject:[NSString stringWithFormat:@"%@",_inforModel.id] forKey:@"productid"];
//    [para setObject:@"2" forKey:@"type"];
//    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
//        if (!_isCollect)
//        {
//            [FTIndicator showSuccessWithMessage:@"收藏成功"];
//        }else
//        {
//            [FTIndicator showSuccessWithMessage:@"取消收藏"];
//        }
//        [selfp refreshCollect:!sender.selected];
//    }];
}


@end
